<?php

namespace App\Http\Middleware;

use App\common;
use App\Models\users;
use Closure;
use Illuminate\Http\Request;

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next, $roles)
    {
        $username = $request->post('username');
        if ($request->filled('username')) {
            $user = users::firstWhere('username', $username);
            if (optional($user)->is_admin != 1) {
                return back()->withInput()->with('danger', '您不是管理员');
            }
        }
        return $next($request);
    }
}
